# Author    : CoffeeChicken
# Date      : 2020-10-04 13:16
# Function  : 演示 抖音去水印

import requests
import re
import json

# 抖音地址
url = 'https://v.douyin.com/JP8EA8d/'
# 请求地址获取视频哈希码
est1 = requests.get(url)
uq = re.findall('video/(\d+)/', str(est1.url))[0]
# 得到哈希码后获取视频地址
ur11 = f'https://www.iesdouyin.com/web/api/v2/aweme/iteminfo/?item_ids={uq}'
est2 = requests.get(ur11).text
js1 = json.loads(est2)
# 找到视频链接后将参数修改获得无水印地址
url3 = str(js1['item_list'][0]['video']['play_addr']['url_list'][0]).replace('playwm', 'play')
# 获得标题
title = str(js1['item_list'][0]['desc'])
# 无水印地址访问时需要用移动端头信息
headers = {
    'User-Agent': 'Mozilla/5.0 (Linux; Android 5.0; SM-G900P Build/LRX21T) AppleWebKit/537.36 (KHTML, like Gecko)'
                  ' Chrome/66.0.3329.0 Mobile Safari/537.36'
}
# est=requests.get(url=url3,headers=headers).url
# 发起请求获得无水印视频
est = requests.get(url=url3, headers=headers)
with open(f'{title}.mp4', 'wb') as f:
    f.write(est.content)
